package com.huayi.server.Dao;

import com.huayi.server.Entity.DeviceEntity;
import com.huayi.server.Repository.DeviceRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceDao {

    Logger logger = LoggerFactory.getLogger(DeviceDao.class);


    @Autowired
    DeviceRepository deviceRepository;

    public void saveAll(List<DeviceEntity> list){
        for (DeviceEntity deviceEntity:list){
            this.save(deviceEntity);
        }
    }

    public void save(DeviceEntity deviceEntity){
        DeviceEntity oldDevice = deviceRepository.getByDeviceId(deviceEntity.getDeviceId());
        if (oldDevice!=null){
            deviceEntity.setId(oldDevice.getId());
        }
        deviceRepository.save(deviceEntity);
    }

    /**
     * 判断数据库当前有没有这个设备id
     * @param deviceId
     * @return
     */
    public Boolean deviceExist(String deviceId){
        DeviceEntity deviceEntity = deviceRepository.getByDeviceId(deviceId);
        return deviceEntity != null;
    }




}
